package itcast.web.servlet;

import itcast.domain.Admin;
import itcast.domain.Buildmanager;
import itcast.domain.Studnet;
import itcast.service.AdminService;
import itcast.service.BuildmanagerService;
import itcast.service.StudentService;
import itcast.service.impl.AdminServiceImpl;
import itcast.service.impl.BuildmanagerServiceimpl;
import itcast.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/dengluServlet")
public class DengluServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.设置编码
        request.setCharacterEncoding("utf-8");
        String te=request.getParameter("table");
        //2.获取数据
        //2.1获取用户填写的验证码
        String verifycode=request.getParameter("verifycode");
        //3.验证码校验
        HttpSession session =request.getSession();
        String checkcode_server=(String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");//确保验证码一次性
        if (!checkcode_server.equalsIgnoreCase(verifycode)){
            //验证码不正确
            //提示信息
            request.setAttribute("denglu_msg","验证码错误!");
            //跳转登录页面
            request.getRequestDispatcher("/systemadmin/denglu.jsp").forward(request,response);
            return;
        }


        if("admin".equals(te)) {

            Map<String, String[]> map = request.getParameterMap();
            //4.封装User对象
            Admin admin = new Admin();
            try {
                BeanUtils.populate(admin, map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }


            //4.调用Service查询
            AdminService service = new AdminServiceImpl();
            Admin dengluAdmin = service.denglu(admin);
            //6.判断是否登录成功
            if (dengluAdmin != null) {
                //登录成功
                //将用户存入session
                session.setAttribute("admin", dengluAdmin);

                //跳转页面
                response.sendRedirect(request.getContextPath()+"/systemadmin/admin_xin.jsp?admin_username="+admin.getUsername());

            }else {
                //登陆失败
                //提示信息
                request.setAttribute("denglu_msg", "用户名和密码错误!");
                //跳转登录页面
                request.getRequestDispatcher("/systemadmin/denglu.jsp").forward(request, response);

                return;
            }
        }else if("buildmanager".equals(te)){

//            Map<String, String[]> map = request.getParameterMap();
            String buildmanager_username=request.getParameter("username");
            String buildmanager_password=request.getParameter("password");
            //4.封装User对象
            Buildmanager buildmanager = new Buildmanager();
           /* try {
                BeanUtils.populate(buildmanager,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }*/


            //4.调用Service查询
            BuildmanagerService service=new BuildmanagerServiceimpl();
          Buildmanager buildmanagerdenlu=service.denglu(buildmanager_username,buildmanager_password);
            //6.判断是否登录成功
            if (buildmanagerdenlu != null) {
                //登录成功
                //将用户存入session
                session.setAttribute("buildmanager",buildmanagerdenlu);
                //跳转页面
                response.sendRedirect(request.getContextPath()+"/dromitory/drom_xin.jsp");
            }else {
                //登陆失败
                //提示信息
                request.setAttribute("denglu_msg", "用户名和密码错误!");
                //跳转登录页面
                request.getRequestDispatcher("/systemadmin/denglu.jsp").forward(request, response);

                return;
            }
        }else if("student".equals(te)){

          /*  Map<String, String[]> map = request.getParameterMap();*/
            //4.封装User对象
            Studnet studnet=new Studnet();
            String student_username=request.getParameter("username");
            String student_password=request.getParameter("password");
           /* try {
                BeanUtils.populate(studnet, map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }*/


            //4.调用Service查询
            StudentService service=new StudentServiceImpl();
           Studnet studnetdenglu=service.studentdenglu(student_username,student_password);
            //6.判断是否登录成功
            if (studnetdenglu!= null) {
                //登录成功
                //将用户存入session
                session.setAttribute("student", studnetdenglu);
                //跳转页面
                response.sendRedirect(request.getContextPath()+"/xuesheng/xuesheng_xin.jsp");
            } else {
                //登陆失败
                //提示信息
                request.setAttribute("denglu_msg", "用户名和密码错误!");
                //跳转登录页面
                request.getRequestDispatcher("/systemadmin/denglu.jsp").forward(request, response);

                return;
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doPost(request,response);
    }
}
